//
//  SmallDecisionsPybViewController.m
//  SmallDecisions
//
//  Created by 李付雄 on 2025/7/25.
//

#import "SmallDecisionsPybViewController.h"
#import "SmallDecisionsSwitchView.h"
#import "SmallDecisionsPkpContainerViewController.h"

@interface SmallDecisionsPybViewController ()<CAAnimationDelegate>

@property (nonatomic, strong) UIImageView *coinImageView;
@property (nonatomic, strong) UIButton *tossButton;
@property (nonatomic, strong) UILabel *headsLabel;
@property (nonatomic, strong) UILabel *tailsLabel;
@property (nonatomic, assign) NSInteger headsCount;
@property (nonatomic, assign) NSInteger tailsCount;
@property (nonatomic, assign) BOOL isAnimating;

@end

@implementation SmallDecisionsPybViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"抛硬币";
    self.edgesForExtendedLayout = UIRectEdgeNone;
    // 初始化计数器
    self.headsCount = 0;
    self.tailsCount = 0;
    self.isAnimating = NO;

    [self coinImageView];
    [self tossButton];
    [self headsLabel];
    [self tailsLabel];
}

- (UIImageView *)coinImageView {
    if(!_coinImageView) {
        // 创建硬币图像视图
        _coinImageView = [[UIImageView alloc] init];
        _coinImageView.image = [UIImage imageNamed:@"heads_img"]; // 默认显示正面
        _coinImageView.contentMode = UIViewContentModeScaleAspectFit;
        [self.view addSubview:_coinImageView];
        _coinImageView.animationImages = @[[UIImage imageNamed:@"heads_img"], [UIImage imageNamed:@"tails_img"]];
        _coinImageView.animationDuration = 1.5;
        _coinImageView.animationRepeatCount = 3;
        [_coinImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(0);
            make.centerY.mas_equalTo(-20);
            make.width.height.mas_equalTo(150);
        }];
    }
    return _coinImageView;
}

- (UILabel *)headsLabel  {
    if(!_headsLabel ) {
        _headsLabel = [[UILabel alloc] init];
        _headsLabel.textColor = SmallDecisionsYellowColor;
        _headsLabel.text = @"正面: 0 次";
        _headsLabel.font = SmallDecisionsSetBoldFontSize(22);
        _headsLabel.textAlignment = 1;
        [self.view addSubview:_headsLabel];
        [_headsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(20);
            make.right.mas_equalTo(self.view.mas_centerX).offset(-10);
        }];
    }
    return _headsLabel;
}

- (UILabel *)tailsLabel  {
    if(!_tailsLabel) {
        _tailsLabel = [[UILabel alloc] init];
        _tailsLabel.textColor = SmallDecisionsYellowColor;
        _tailsLabel.text = @"反面: 0 次";
        _tailsLabel.font = SmallDecisionsSetBoldFontSize(22);
        _tailsLabel.textAlignment = 1;
        [self.view addSubview:_tailsLabel];
        [_tailsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.headsLabel);
            make.left.mas_equalTo(self.view.mas_centerX).offset(10);
        }];
    }
    return _tailsLabel;
}


- (UIButton *)tossButton  {
    if(!_tossButton ) {
        _tossButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_tossButton setTitle:@"抛一下" forState:UIControlStateNormal];
        [_tossButton setTitleColor:SmallDecisionsThemeFontColor forState:UIControlStateNormal];
        [_tossButton setTitleColor:[UIColor clearColor] forState:UIControlStateHighlighted];
        _tossButton.titleLabel.font = SmallDecisionsSetBoldFontSize(16);
        _tossButton.layer.cornerRadius = 30;
        _tossButton.layer.masksToBounds = YES;
        _tossButton.backgroundColor = SmallDecisionsThemeColor;
        [_tossButton addTarget:self action:@selector(tossButtonAction) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_tossButton];
        [_tossButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(60);
            make.left.mas_equalTo(60);
            make.right.mas_equalTo(-60);
            make.bottom.mas_equalTo(-self.view.safeAreaInsets.bottom - 30);
        }];
    }
    return _tossButton;
}

- (void)tossButtonAction {
    if (self.isAnimating) {
        return; // 防止动画期间重复点击
    }

    self.isAnimating = YES;
    self.tossButton.enabled = NO;

    // 随机决定最终是正面还是反面
    BOOL isHeads = arc4random_uniform(2) == 0;

    // 动画持续时间
    CGFloat duration = 2.0;

    // 旋转动画
    CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.x"];
    rotationAnimation.fromValue = @(0);
    rotationAnimation.toValue = @(M_PI * 10); // 旋转5圈(2π * 5)
    rotationAnimation.duration = duration;

    // 缩放动画 - 抛起和落下
    CABasicAnimation *scaleUpAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    scaleUpAnimation.fromValue = @(1.0);
    scaleUpAnimation.toValue = @(1.2);
    scaleUpAnimation.duration = duration/2;
    scaleUpAnimation.autoreverses = YES;

    // 位置动画 - 上下移动
    CABasicAnimation *positionAnimation = [CABasicAnimation animationWithKeyPath:@"position.y"];
    positionAnimation.fromValue = @(self.coinImageView.center.y);
    positionAnimation.toValue = @(self.coinImageView.center.y - 140);
    positionAnimation.duration = duration/2;
    positionAnimation.autoreverses = YES;

    // 组合动画
    CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
    animationGroup.animations = @[rotationAnimation, scaleUpAnimation, positionAnimation];
    animationGroup.duration = duration;
    animationGroup.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

    // 设置动画完成后的回调
    animationGroup.delegate = self;
    [animationGroup setValue:@(isHeads) forKey:@"isHeads"];

    [self.coinImageView.layer addAnimation:animationGroup forKey:@"coinTossAnimation"];
}

- (void)animationDidStart:(CAAnimation *)anim {
    [self.coinImageView startAnimating];
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    if (flag) {
        [self.coinImageView stopAnimating];
        // 获取存储在动画中的结果
        BOOL isHeads = [[anim valueForKey:@"isHeads"] boolValue];
        // 更新硬币图像
        if (isHeads) {
            self.coinImageView.image = [UIImage imageNamed:@"heads_img"];
            self.headsCount++;
            self.headsLabel.text = [NSString stringWithFormat:@"正面: %ld 次", (long)self.headsCount];
        } else {
            self.coinImageView.image = [UIImage imageNamed:@"tails_img"];
            self.tailsCount++;
            self.tailsLabel.text = [NSString stringWithFormat:@"反面: %ld 次", (long)self.tailsCount];
        }
        self.isAnimating = NO;
        self.tossButton.enabled = YES;
    }
}


- (UIView *)setRightBarButton {
    UIButton *addItemsBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [addItemsBtn setTitle:@"重置" forState:UIControlStateNormal];
    addItemsBtn.titleLabel.font = SmallDecisionsSetFontSize(13);
    [addItemsBtn setTitleColor:SmallDecisionsThemeFontColor forState:UIControlStateNormal];
    addItemsBtn.frame = CGRectMake(0, 0, 30, 30);
    return addItemsBtn;
}

- (void)rightBarButtonAction:(UIButton *)view {
    self.headsCount = 0;
    self.tailsCount = 0;
    self.headsLabel.text = [NSString stringWithFormat:@"正面: %ld 次", (long)self.headsCount];
    self.tailsLabel.text = [NSString stringWithFormat:@"反面: %ld 次", (long)self.tailsCount];
    self.coinImageView.image = [UIImage imageNamed:@"heads_img"]; // 默认显示正面
}

@end
